﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace caufingHealth.Forms
{
    public partial class LoginForm : Form
    {
        //Available Hash Algorithms
        public enum ENCRYPTION_LEVEL {Sha1, Sha256, Sha512, none};

        private ENCRYPTION_LEVEL eLevel;

        /// <summary>
        /// Stores the User Name entered into the Login field.
        /// </summary>
        public string UserName
        {
            get;
            private set;
        }

        /// <summary>
        /// Stores the Hashed string's value.
        /// This field is populated only if ENCRYPTION_LEVEL
        /// specified is something other than NONE.
        /// </summary>
        public string SecurePassword
        {
            get;
            private set;
        }

        /// <summary>
        /// Plain-Text passwords will be stored here.
        /// This field is only populated if the ENCRYPTION_LEVEL
        /// specified is NONE.
        /// </summary>
        public string Password
        {
            get;
            private set;
        }

        /// <summary>
        /// Constructs the LoginForm and sets the HashAlgorithm
        /// that will be used to secure the password.
        /// </summary>
        /// <param name="encrypt"></param>
        public LoginForm(ENCRYPTION_LEVEL encrypt)
        {
            InitializeComponent();
            eLevel = encrypt;
            btn_Login.Enabled = false;
        }

        /// <summary>
        /// Handles to 'Login' button click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Login_Click(object sender, EventArgs e)
        {
                UserName = txt_UserName.Text;

                if (eLevel == ENCRYPTION_LEVEL.none)
                    Password = txt_Password.Text;
                else
                    Encryptor(txt_Password.Text);
        }

        /// <summary>
        /// Encrypts the string into a hash value
        /// using a set of hash algorithms.
        /// </summary>
        /// <param name="data">the string to be hashed</param>
        private void Encryptor(String data)
        {
            HashAlgorithm encryptor = null;

            switch (eLevel)
            {
                case ENCRYPTION_LEVEL.Sha512:
                    encryptor = SHA512.Create();
                    break;
                case ENCRYPTION_LEVEL.Sha256:
                    encryptor = SHA512.Create();
                    break;
                case ENCRYPTION_LEVEL.Sha1:
                    encryptor = SHA1.Create();
                    break;
            }

            SecurePassword = ByteArrayToString(encryptor.ComputeHash(ConvertToByteArray(data)));
        //    Console.WriteLine(SecurePassword);
     //       MessageBox.Show(SecurePassword);
        }

        /// <summary>
        /// Converts to a string to a Byte array for the purpose
        /// of converting to a hash value.
        /// </summary>
        /// <param name="data">the string to be converted</param>
        /// <returns>Byte array representation of the string</returns>
        private Byte[] ConvertToByteArray(string data)
        {    
            ASCIIEncoding encoding = new ASCIIEncoding();
            return encoding.GetBytes(data);
        }

        /// <summary>
        /// Converts a Byte array to a string.
        /// </summary>
        /// <param name="hashArray">a Byte array to convert to a string</param>
        /// <returns>the string representation of the Byte array</returns>
        private string ByteArrayToString(Byte[] hashArray)
        {
            StringBuilder hash = new StringBuilder("");
            foreach (Byte hashbyte in hashArray)
            {
                hash.Append(hashbyte.ToString("X2"));
            }
            return hash.ToString();
        }

        private void txt_UserName_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int.Parse(((TextBox)sender).Text);
            }
            catch (FormatException)
            {
                if (!String.IsNullOrEmpty(((TextBox)sender).Text))
                {
                    MessageBox.Show("The UserID is your numerical employee id.");
                    ((TextBox)sender).Text = "";
                    txt_UserName.Focus();
                }
            }
        }

        private void txt_UserName_Validated(object sender, EventArgs e)
        {

        }

        private void txt_Password_Validating(object sender, CancelEventArgs e)
        {

        }

        private void txt_Password_Validated(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txt_UserName.Text))
                btn_Login.Enabled = true;
        }

        private void txt_Password_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txt_UserName.Text) || txt_Password.Text.Length == 0)
                btn_Login.Enabled = false;
            else
                btn_Login.Enabled = true;
        }

        private void txt_UserName_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(((TextBox)sender).Text))
                btn_Login.Enabled = false;
            else
                if (!String.IsNullOrEmpty(txt_Password.Text))
                    btn_Login.Enabled = true;
                else
                    btn_Login.Enabled = false;

        }
    }
}
